package view.GUI.adminView.invoiceOverview;

import java.util.ArrayList;
import java.util.List;

import view.GUI.mainView.MainView;
import model.Configuration;
import model.Utility;
import model.data.tableModel.sale.Bill;
import enums.GUIText;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public final class InvoiceOverview extends Application {

	 private TabPane tabPane;
	 private Tab tabOverView;
	 private Tab tabHistory;
	 
	 private Overview overview;
	 private InvoiceHistory history;
	 
	 private ScrollPane sp;
	 
	 private Stage primaryStage;
	 private Scene scene;
	 
	 private MainView mainview;	 
	 
	 public InvoiceOverview(MainView mainview){
		 this.mainview = mainview;
		 this.primaryStage = new Stage();
		 createWidgets();
		 addContentToHistory();
		 addWidgets();
		 setupInteraction();
		 
		 try {
			start(primaryStage);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		 
	 }
		
	private void addContentToHistory() {
		List<Bill> billList = mainview.getBills();
		
		List<Bill> tempBillList = new ArrayList<>();
		String year = null;
		for(Bill b: billList){
			
			if(year != null && !year.equals(b.getDate().substring(0, 4))){
				history.addInvoice(year, tempBillList);
				tempBillList.clear();
				year = b.getDate().substring(0, 4);
				tempBillList.add(b);
			} else{
				year = b.getDate().substring(0, 4);
				tempBillList.add(b);
			}
		}
		history.addInvoice(year, tempBillList);
	}

	private void setupInteraction() {
		primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>(){

			@Override
			public void handle(WindowEvent event) {
				overview.saveBills();
			}
		});
	}

	private void addWidgets() {
		sp.setContent(tabPane);
		tabOverView.setContent(overview);
		tabHistory.setContent(history);
		
		tabPane.getTabs().addAll(tabOverView, tabHistory);
	}

	private void createWidgets() {
		overview = new Overview(mainview.getActiveDebts(), primaryStage);
		history = new InvoiceHistory();
		sp = new ScrollPane();
		tabPane = new TabPane();
		tabOverView = new Tab(GUIText.OVERVIEW.toString());
		tabHistory = new Tab(GUIText.HISTORY.toString());

	}
	

	@Override
	public void start(Stage primaryStage) throws Exception {
		scene = new Scene(sp);
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEOVERVIEW).toExternalForm());
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEROOT).toExternalForm());
		this.primaryStage = primaryStage;
		this.primaryStage.initModality(Modality.WINDOW_MODAL);
		primaryStage.initOwner(mainview.getPrimaryStage());
		primaryStage.setScene(scene);
		primaryStage.setTitle(Utility.concatStrings(GUIText.INVOICE.toString(), " ", GUIText.OVERVIEW.toString()));
		primaryStage.setResizable(true);
		primaryStage.centerOnScreen();
		primaryStage.show();
	}
}
